package com.jhx.common.utils;

import android.content.Context;
import android.text.TextUtils;

import com.jhx.common.cache.SimpleCache;
import com.jhx.common.model.User;

/**
 * 存储用户数据管理类
 *
 * @author wenqin 2017-06-07 10:59
 */

public class UserManager {
    private static volatile UserManager sManager;
    private static String KEY_DISCOUNT = "key_discount";

    public static UserManager get() {
        if (sManager == null) {
            synchronized (UserManager.class) {
                if (sManager == null) {
                    sManager = new UserManager();
                }
            }
        }
        return sManager;
    }

    public void clear() {
        SimpleCache.Companion.delete(KEY_DISCOUNT);
        SimpleCache.Companion.delete(Constants.USER_INFO);
    }

    public boolean checkUserNull(Context context) {
        if (getUser() == null) {
            ToastUtil.getInstance().showToast("获取用户信息失败，请重新登录");
            return true;
        }
        return false;
    }

    public void setUser(User user) {
        if (user != null) {
            SimpleCache.Companion.put(Constants.USER_INFO, user);
        }
    }


    public User getUser() {
        return (User) SimpleCache.Companion.get(Constants.USER_INFO);
    }

    public UserManager setLicense(String license) {
        if (getUser() != null && !TextUtils.isEmpty(license)) {
           User user = getUser();
           user.setLicense(license);
           setUser(user);
        }
        return this;
    }

    public UserManager setBusinessName(String businessName) {
        if (getUser() != null && !TextUtils.isEmpty(businessName)) {
            User user = getUser();
            user.setBusinessName(businessName);
            setUser(user);
        }
        return this;
    }

    public UserManager setRegisterNum(String registerNum) {
        if (getUser() != null&& !TextUtils.isEmpty(registerNum)) {
            User user = getUser();
            user.setRegisterNum(registerNum);
            setUser(user);
        }
        return this;
    }

    public UserManager setLocalLicense(String localLicense) {
        if (getUser() != null && !TextUtils.isEmpty(localLicense)) {
            User user = getUser();
            user.setLocalLicense(localLicense);
            setUser(user);
        }
        return this;
    }

    public UserManager setLegalIDfront(String legalIDfront) {
        if (getUser() != null && !TextUtils.isEmpty(legalIDfront)) {
            User user = getUser();
            user.setLegalIDfront(legalIDfront);
            setUser(user);
        }
        return this;
    }

    public UserManager setLegalIDback(String legalIDback) {
        if (getUser() != null && !TextUtils.isEmpty(legalIDback)) {
            User user = getUser();
            user.setLegalIDback(legalIDback);
            setUser(user);
        }
        return this;
    }

    public UserManager setLocalIDback(String localIDback) {
        if (getUser() != null && !TextUtils.isEmpty(localIDback)) {
            User user = getUser();
            user.setLocalIDback(localIDback);
            setUser(user);
        }
        return this;
    }

    public UserManager setLocalIDfront(String localIDfront) {
        if (getUser() != null && !TextUtils.isEmpty(localIDfront)) {
            User user = getUser();
            user.setLocalIDfront(localIDfront);
            setUser(user);
        }
        return this;
    }

    public UserManager setLegal(String legal) {
        if (getUser() != null && !TextUtils.isEmpty(legal)) {
            User user = getUser();
            user.setLegal(legal);
            setUser(user);
        }
        return this;
    }

    public UserManager setIDCard(String idCard) {
        if (getUser() != null && !TextUtils.isEmpty(idCard)) {
            User user = getUser();
            user.setIDCard(idCard);
            setUser(user);
        }
        return this;
    }

    public UserManager setDiscount(String discount) {
        if(!TextUtils.isEmpty(discount)) {
            SimpleCache.Companion.put(KEY_DISCOUNT, discount);
        }
        return this;
    }

    public String getDiscount() {
        try {
            return SimpleCache.Companion.get(KEY_DISCOUNT);
        } catch (Exception e) {
            return "0";
        }
    }

    public UserManager setOneCategoryId(String categoryId) {
        if (getUser() != null) {
            User user = getUser();
            user.setOneCategoryId(categoryId);
            setUser(user);
        }
        return this;
    }

    public UserManager setCategoryName(String categoryName) {
        if (getUser() != null && !TextUtils.isEmpty(categoryName)) {
            User user = getUser();
            user.setCategoryName(categoryName);
            setUser(user);
        }
        return this;
    }

    public UserManager setCityId(long cityId) {
        if (getUser() != null && cityId > 0) {
            User user = getUser();
            user.setCityId(cityId);
            setUser(user);
        }
        return this;
    }

    public UserManager setProvinceId(long provinceId) {
        if (getUser() != null && provinceId > 0) {
            User user = getUser();
            user.setProvinceId(provinceId);
            setUser(user);
        }
        return this;
    }

    public UserManager setTownId(long townId) {
        if (getUser() != null && townId > 0) {
            User user = getUser();
            user.setTownId(townId);
            setUser(user);
        }
        return this;
    }

    public UserManager setProvinceName(String provinceName) {
        if (getUser() != null && !TextUtils.isEmpty(provinceName)) {
            User user = getUser();
            user.setProvinceName(provinceName);
            setUser(user);
        }
        return this;
    }

    public UserManager setCityName(String cityName) {
        if (getUser() != null && !TextUtils.isEmpty(cityName)) {
            User user = getUser();
            user.setCityName(cityName);
            setUser(user);
        }
        return this;
    }

    public UserManager setTownName(String townName) {
        if (getUser() != null && !TextUtils.isEmpty(townName)) {
            User user = getUser();
            user.setTownName(townName);
            setUser(user);
        }
        return this;
    }

    public UserManager setShopPhoto(String shopPhoto) {
        if (getUser() != null && !TextUtils.isEmpty(shopPhoto)) {
            User user = getUser();
            user.setShopPhoto(shopPhoto);
            setUser(user);
        }
        return this;
    }

    public UserManager setArea(String area) {
        if(getUser() != null && !TextUtils.isEmpty(area)) {
            User user = getUser();
            user.setArea(area);
            setUser(user);
        }
        return this;
    }

    public UserManager setAddress(String address) {
        if (getUser() != null && !TextUtils.isEmpty(address)) {
            User user = getUser();
            user.setAddress(address);
            setUser(user);
        }
        return this;
    }

    public UserManager setShopAddress(String shopAddress) {
        if (getUser() != null && !TextUtils.isEmpty(shopAddress)) {
            User user = getUser();
            user.setShopAdd(shopAddress);
            setUser(user);
        }
        return this;
    }

    public UserManager setLatitude(double latitude) {
        if (getUser() != null) {
            User user = getUser();
            user.setLatitude(latitude);
            setUser(user);
        }
        return this;
    }

    public UserManager setLongitude(double longitude) {
        if (getUser() != null ) {
            User user = getUser();
            user.setLongitude(longitude);
            setUser(user);
        }
        return this;
    }

    public UserManager setLocalShopPhoto(String localShopPhoto) {
        if (getUser() != null && !TextUtils.isEmpty(localShopPhoto)) {
            User user = getUser();
            user.setLocalShopPhoto(localShopPhoto);
            setUser(user);
        }
        return this;
    }

    public UserManager setBankUser(String bankUser) {
        if (getUser() != null && !TextUtils.isEmpty(bankUser)) {
            User user = getUser();
            user.setBankUser(bankUser);
            setUser(user);
        }
        return this;
    }

    public UserManager setBankNum(String bankNum) {
        if (getUser() != null && !TextUtils.isEmpty(bankNum)) {
            User user = getUser();
            user.setBankNum(bankNum);
            setUser(user);
        }
        return this;
    }

    public UserManager setBankName(String bankName) {
        if (getUser() != null && !TextUtils.isEmpty(bankName)) {
            User user = getUser();
            user.setBankName(bankName);
            setUser(user);
        }
        return this;
    }
}